package com.java.imauoj.controler;

import com.java.imauoj.Tools.Function;
import com.java.imauoj.moudel.*;
import com.java.imauoj.sercive.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("api/v1")
public class UserControler {

    @Autowired
    private UserService service;
      //获取用户信息
    @PostMapping("getUserList")
    public ResultVo getUserList(@RequestBody Filter filter, HttpSession session){
        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_get_user() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                re.setMessage("成功");
                Function function = new Function();
                FanHuiZhi fhz = function.qinjie(filter);
                re.setData(service.getUserList(fhz));
            }
        }
        return re;
    }
        //用户的操作
    @PostMapping(value = "userOperate")
    public ResultVo userOperate(@RequestParam(value = "active") int active,@RequestBody User user, HttpSession session){

        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_ope_user() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                if(active == 0){
                    User user1 = service.updateUser(user);
                    re.setMessage("修改成功");
                    re.setData(user1);
                }else if(active == 1){
                    User user1 = service.addUser(user);
                    re.setMessage("添加成功");
                    re.setData(user1);
                }else if(active == -1){
                    User user1 = service.deleteUser(user.getUser_id());
                    re.setMessage("删除成功");
                    re.setData(user1);
                }
            }
        }
        return re;
    }

}
